#include "barmeter.h"

//--------------------------------------------------------------------------
// BarMeter::BarMeter
//--------------------------------------------------------------------------
BarMeter::BarMeter(QWidget *parent)
  : QWidget(parent)
{                                                       // constructor
  setBackgroundRole(QPalette::Base);
  setAutoFillBackground(true);

  m_level1 = 0;
  m_level2 = 0;
}

//--------------------------------------------------------------------------
// BarMeter::paintEvent()
//--------------------------------------------------------------------------
void BarMeter::paintEvent(QPaintEvent * /* event */)
{
  int x0=0;
  int y0=4;
  QPainter painter(this);

  painter.setPen(Qt::black);
  painter.drawRect(QRect(painter.viewport().left()+x0,
                         painter.viewport().top()+y0,
                         painter.viewport().right()-10,
                         painter.viewport().bottom()-10));

  if (m_level1==0.0 && m_level2==0.0) return;

  painter.setPen(Qt::green);
  int pos = ((painter.viewport().right()-20) -
             (painter.viewport().left()+11))*m_level1;
  for (int i = 0; i < 10; ++i) {
    int x1 = painter.viewport().left()+x0+1;
    int y1 = painter.viewport().top()+y0+1+i;
    int x2 = painter.viewport().left()+x0+10+pos;
    int y2 = painter.viewport().top()+y0+1+i;
    if (x2 < painter.viewport().left()+x0)x2 = painter.viewport().left()+10;
    painter.drawLine(QPoint(x1, y1),QPoint(x2, y2));
  }

  painter.setPen(Qt::magenta);
  pos = ((painter.viewport().right()-20) -
             (painter.viewport().left()+x0+1))*m_level2;
  for (int i = 0; i < 10; ++i) {
    int x1 = painter.viewport().left()+x0+1;
    int y1 = painter.viewport().top()+y0+11+i;
    int x2 = painter.viewport().left()+x0+10+pos;
    int y2 = painter.viewport().top()+y0+11+i;
    if (x2 < painter.viewport().left()+x0)x2 = painter.viewport().left()+10;
    painter.drawLine(QPoint(x1, y1),QPoint(x2, y2));
  }

}

//--------------------------------------------------------------------------
// BarMeter::setLevel()
//--------------------------------------------------------------------------
void BarMeter::setLevel(qreal value1, qreal value2)
{
  m_level1 = value1;
  m_level2 = value2;
  repaint();
}
